
local locplayer = ac.player.self
local w,h = 420,100
local ui = class.panel:builder 
{
    x = (1920 - w) / 2,
    y = 805,
    w = w,
    h = h,
    alpha = 0.99,
    level = 99,
    --show_flag = false,
    --fdf = 'tooltip_backdrop0',
}

local w1,h1 = 64,64
local x1 = 15

local cost = ui:add_panel('',x1,15,ui.w - x1*2 - 3,8)
ui.cost = cost
cost.back = cost:add_panel([[core\hpbar\ShieldBar.tga]])
cost.fill = cost:add_panel([[core\hpbar\红条.tga]])
cost.last_load = 0
cost.buffer_rate = 0
cost.back:set_alpha(0.5)

--设置进度
function cost:set_load(rate)
    self.last_load = rate
    if rate==0 then
        self.buffer_rate = 0
    end
end

function cost:update_load()
    local load1 = math.max(math.abs(self.last_load - self.buffer_rate)*0.05,0.01)
    if self.last_load>=self.buffer_rate then
        self.buffer_rate = math.min(self.buffer_rate + load1,self.last_load)
        self.fill:set_width(self.w*self.buffer_rate + 0.01)
    elseif self.last_load<self.buffer_rate then
        self.buffer_rate = math.max(self.buffer_rate - load1,self.last_load)
        self.fill:set_width(self.w* self.buffer_rate + 0.01)
    end
end


ui.buttons = {}
local images = {
    [[ReplaceableTextures\CommandButtons\BTNBlizzard.blp]],
    [[ReplaceableTextures\CommandButtons\BTNStormBolt.blp]],
    [[ReplaceableTextures\CommandButtons\BTNResurrection.blp]],
    [[ReplaceableTextures\CommandButtons\BTNMarkOfFire.blp]],
    [[ReplaceableTextures\CommandButtons\BTNThunderclap.blp]],
    [[ReplaceableTextures\CommandButtons\BTNWallOfFire.blp]],
}
local normal_icon = [[UI\ui_b0.tga]]
for a=1,6 do
    local x = (a-1) * (w1 + 1) + x1
    local panel = ui:add_button(normal_icon,x,25,w1,h1)
    panel:add_panel(normal_icon)
    panel:set_normal_image(images[a])
    table.insert(ui.buttons,panel)
end
ui.select = ui:add_highlight([[UI\Widgets\Console\Human\CommandButton\human-activebutton.blp]],0,0,w1,h1)
ui.select:hide()

--离开
ui.show_time = 0

function ui:update(frame)
    self.cost:update_load()
    local data = locplayer:get_data('预设技能')
    for i,button in ipairs(self.buttons) do
        local skill = data and data[i]
        if skill then
            button.skill = skill
            button:set_normal_image(skill.art)
        else
            button:set_normal_image([[textures\black32.blp]])
        end
    end
    
end

function ui:on_button_mouse_enter(button)
    local skill = button.skill
    if skill then
        local title = format_obj(skill,skill.title or skill.name)
        local tip = format_obj(skill,skill.tip)
        local ui = button:tooltip(title,tip,'top')
        --self:set_tooltip_follow(ui, 'right',0,-(self.h-ui.h)/2)
    end
end

--[设置技能进度]
ac.game:event 'UI-设置预设技能进度'(function(_,player,rate,speed)
    if player:is_self() then
        cost:set_load(rate)
    end
end)

--[触发预设技能]
ac.game:event 'UI-触发预设技能'(function(_,player,cast_skill)
    if player:is_self() then
        for i,button in ipairs(ui.buttons) do
            local skill = button.skill
            if skill==cast_skill then
                show_panel_effect(player,button,'刷新')
            end
        end
    end
end)

--[触发预设技能]
ac.game:event 'UI-准备预设技能'(function(_,player,cast_skill)
    if player:is_self() then
        cost:set_load(0)
        for i,button in ipairs(ui.buttons) do
            local skill = button.skill
            if skill==cast_skill then
                local x,y = button:get_real_position()
                ui.select:set_real_position(x,y)
                ui.select:show()
            end
        end
    end
end)

ac.game:notify('游戏-注册显示UI',ui)

return ui